package aps

import com.alibaba.fastjson.JSONObject
import com.fasterxml.jackson.dataformat.xml.XmlMapper
import glory.xxd.Utils
import glory.xxd.XmlUtils
import grails.converters.JSON


class TestController {
    def linkList = [[msgId: 1122, ISALERT: true], [msgId: 1121, ISALERT: false], [msgId: 1123, ISALERT: true], [msgId: 1124, ISALERT: true]]

    def alertList = new LinkedList()

    def index() {
        def result = linkList.getAt(0..2)

        for (Map data : result) {
            def msgId = data?.msgId
            if (!alertList.contains(msgId)) {
                alertList.addLast(msgId)
            } else {
                //data.remove("ISALERT")
                data.put("ISALERT", false)
            }

            if (alertList.size() > 5) {
                alertList.removeFirst()
            }
        }

        println linkList
        println result
        println alertList
    }


//    def test() {
//        public String[] DATANAME;
//        public String[] DATAVALUE;
//
//        JSONObject jsonObj_data;//用来接收解析的JSON字符串
//
//        JSONArray jsonArr_data;//用来接收JSON对象里的数组
//
//        try {
//            jsonObj_data = new JSONObject(Value); //解析JSON字符串
//            jsonArr_data = jsonObj_data.getJSONArray("INPUTDATALIST");//接收JSON对象里的数组
//            int jsonSize_data = jsonArr_data.length();//获取数组长度
//            DATANAME = new String[jsonSize_data];//初始化数组
//            DATAVALUE = new String[jsonSize_data];//初始化数组
//            for (int i = 0; i < jsonSize_data; i++)//通过循环取出数组里的值
//            {
//                JSONObject jsonTemp = (JSONObject) jsonArr_data.getJSONObject(i);
//                DATANAME[i] = jsonTemp.getString("DATANAME");
//                DATAVALUE[i] = jsonTemp.getString("DATAVALUE");
//            }
//
//        }
//        catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    def test1() {


        def resultJSON = org.codehaus.groovy.grails.web.json.JSONObject.newInstance()

        def jsonStr = """
[{"DATACNNAME":"中文名1","DATANAME":"ParamName","DATAVALUE":"1111","ISNULL":"Y"},{"DATACNNAME":"中文名","DATANAME":"ParamName","DATAVALUE":"111","ISNULL":"Y"}]

"""
        def json = JSON.parse(jsonStr)

        resultJSON.put("INPUTDATALIST", json)

        render resultJSON.toString()
    }

    def testJson() {

        def r = Math.random()

        def xml = "<Response>\n" +
                "    <Header>\n" +
                "        <TRANSACTIONID>e1e91d88-bce3-49f3-87bd-c4b8d0af7db4</TRANSACTIONID>\n" +
                "        <MESSAGENAME>SendEQPMeterial</MESSAGENAME>\n" +
                "        <USERNAME>EAP</USERNAME>\n" +
                "    </Header>\n" +
                "    <Body>\n" +
                "        <EqpId>1FS05</EqpId>\n" +
                "        <Materials>\n" +
                "            <MesMaterial>\n" +
                "                <ID>H180206KY010</ID>\n" +
                "                <MaterialPartNo>ZH05-SD3000-N1-50 BB</MaterialPartNo>\n" +
                "                <Type>WaferSaw</Type>\n" +
                "                <SeqNo>H180206KY010</SeqNo>\n" +
                "                <Size>undefined</Size>\n" +
                "                <SawLength>0.57</SawLength>\n" +
                "                <SawThickness>0.024</SawThickness>\n" +
                "                <InternalDiameter>000000</InternalDiameter>\n" +
                "                <ExternalDiameter>20190311</ExternalDiameter>\n" +
                "                <EqpID>1FS05</EqpID>\n" +
                "                <Qty>0</Qty>\n" +
                "            </MesMaterial>\n" +
                "        </Materials>\n" +
                "\n" +
                "    </Body>\n" +
                "</Response>";

        def json = XmlUtils.xml2Json(xml)

//        print json.getClass().toString();
        JSONObject Response = json.Response ?: ''

        JSONObject Body = Response.get("Body")

        render Body
    }


    def index3() {
    println "index3"
        def xml = """
 <Response>
    <Header>
        <TRANSACTIONID>e1e91d88-bce3-49f3-87bd-c4b8d0af7db4</TRANSACTIONID>
        <MESSAGENAME>SendEQPMeterial</MESSAGENAME>
        <USERNAME>EAP</USERNAME>
         <RESULT>SUCCESS</RESULT>
    </Header>
    <Body>
        <EqpId>1FS05</EqpId>
        <Materials>
            <MesMaterial>
                <ID>H180206KY010</ID>
                <MaterialPartNo>ZH05-SD3000-N1-50 BB</MaterialPartNo>
                <Type>WaferSaw</Type>
                <SeqNo>H180206KY010</SeqNo>
                <Size>undefined</Size>
                <SawLength>0.57</SawLength>
                <SawThickness>0.024</SawThickness>
                <InternalDiameter>undefined</InternalDiameter>
                <ExternalDiameter>undefined</ExternalDiameter>
                <EqpID>1FS05</EqpID>
                <Qty>001</Qty>
            </MesMaterial>
 <MesMaterial>
                <ID>H180206KY010</ID>
                <MaterialPartNo>ZH05-SD3000-N1-50 BB</MaterialPartNo>
                <Type>WaferSaw</Type>
                <SeqNo>H180206KY010</SeqNo>
                <Size>undefined</Size>
                <SawLength>0.57</SawLength>
                <SawThickness>0.024</SawThickness>
                <InternalDiameter>undefined</InternalDiameter>
                <ExternalDiameter>undefined</ExternalDiameter>
                <EqpID>1FS05</EqpID>
                <Qty>001</Qty>
            </MesMaterial>
        </Materials>
    </Body>
</Response>


"""

        def json = Utils.getJsonObjectByXML_1(xml)
        render json.toString()
//        def standardJSONForPDA = org.json.JSONObject.newInstance()

//        standardJSONForPDA.put("data", json)
//        standardJSONForPDA.put("code", 200)


        // render json.toString()

    }
}
